package com.vimeworks.vimemannager.web.controlador;

import java.io.IOException;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ControladorBase {

	protected void agregarMensajeError(String resumen, String detalle) {
		FacesMessage errorMessage = new FacesMessage();
		errorMessage.setSummary(resumen);
		errorMessage.setDetail(detalle == null ? detalle : detalle.toUpperCase());
		errorMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
		FacesContext.getCurrentInstance().addMessage(null, errorMessage);
	}

	protected void agregarMensajeError(String id, String resumen, String detalle) {
		FacesMessage errorMessage = new FacesMessage();
		errorMessage.setSummary(resumen);
		errorMessage.setDetail(detalle == null ? detalle : detalle.toUpperCase());
		errorMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
		FacesContext.getCurrentInstance().addMessage(id, errorMessage);
	}

	protected void agregarMensajeInformacion(String resumen, String detalle) {
		FacesMessage infoMessage = new FacesMessage();
		infoMessage.setSummary(resumen.toUpperCase());
		infoMessage.setDetail(detalle == null ? detalle : detalle.toUpperCase());
		infoMessage.setSeverity(FacesMessage.SEVERITY_INFO);
		FacesContext.getCurrentInstance().addMessage(null, infoMessage);
	}

	protected void agregarMensajeInformacion(String id, String resumen, String detalle) {
		FacesMessage infoMessage = new FacesMessage();
		infoMessage.setSummary(resumen.toUpperCase());
		infoMessage.setDetail(detalle == null ? detalle : detalle.toUpperCase());
		infoMessage.setSeverity(FacesMessage.SEVERITY_INFO);
		FacesContext.getCurrentInstance().addMessage(id, infoMessage);
	}
	
	protected void agregarMensajeAdvertencia(String resumen, String detalle) {
		FacesMessage infoMessage = new FacesMessage();
		infoMessage.setSummary(resumen.toUpperCase());
		infoMessage.setDetail(detalle == null ? detalle : detalle.toUpperCase());
		infoMessage.setSeverity(FacesMessage.SEVERITY_INFO);
		FacesContext.getCurrentInstance().addMessage(null, infoMessage);
	}

	protected void agregarMensajeAdvertencia(String id, String resumen, String detalle) {
		FacesMessage infoMessage = new FacesMessage();
		infoMessage.setSummary(resumen.toUpperCase());
		infoMessage.setDetail(detalle == null ? detalle : detalle.toUpperCase());
		infoMessage.setSeverity(FacesMessage.SEVERITY_WARN);
		FacesContext.getCurrentInstance().addMessage(id, infoMessage);
	}
	
	protected void limpiarMensajes() {
		//FacesContext.getCurrentInstance().
	}

	/**
	 * @return
	 */
	public HttpServletRequest getHttpRequest() {
		return (HttpServletRequest) getExternalContext().getRequest();
	}

	/**
	 * Obtiene el valor de request parameter.
	 * 
	 * @param parameter
	 *            el valor de parameter
	 * 
	 * @return valor de request parameter
	 */
	protected String getHttpRequestParameter(final String parameter) {
		return FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap().get(parameter);
	}

	/**
	 * @return
	 */
	public HttpServletResponse getHttpResponse() {
		return (HttpServletResponse) getExternalContext().getResponse();
	}

	/**
	 * @return
	 */
	protected HttpSession getHttpSession() {
		return (HttpSession) getExternalContext().getSession(false);
	}

	protected ServletContext getServletContext() {
		return (ServletContext) getExternalContext().getContext();
	}

	protected ExternalContext getExternalContext() {
		return FacesContext.getCurrentInstance().getExternalContext();
	}

	public String getMessagesStyleClass() {
		if (FacesContext.getCurrentInstance().getMaximumSeverity() != null) {
			int maximumSeverityOrdinal = FacesContext.getCurrentInstance()
					.getMaximumSeverity().getOrdinal();
			if (maximumSeverityOrdinal == FacesMessage.SEVERITY_ERROR.getOrdinal()) {
				return "mensajeError";
			} else if (maximumSeverityOrdinal == FacesMessage.SEVERITY_INFO.getOrdinal()) {
				return "mensajeInformacion";
			} else if (maximumSeverityOrdinal == FacesMessage.SEVERITY_WARN.getOrdinal()) {
				return "mensajeAdvertencia";
			}
		}
		return "messages-hidden";
	}

	/**
	 * Obtiene el valor de context path.
	 * 
	 * @return valor de context path
	 */
	public String getContextPath() {
		return FacesContext.getCurrentInstance().getExternalContext()
				.getRequestContextPath();
	}


	public Map<String, Object> getSessionMap() {
		return FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
	}

	protected void redireccionarPagina(String pagina) {
		try {
			getExternalContext().redirect(
					getHttpRequest().getContextPath().concat(pagina));
		} catch (IOException e) {
			agregarMensajeError(" ---> No se puede redireccionar a " + pagina,e.getLocalizedMessage());
		}
	}

	public Boolean getExisteMensaje() {
		return FacesContext.getCurrentInstance().getMaximumSeverity() != null
				&& FacesContext.getCurrentInstance().getMaximumSeverity()
						.equals(FacesMessage.SEVERITY_ERROR);
	}
	
	public String obtenerUsuarioSesion(){
		return (String)getHttpSession().getAttribute("ID_USUARIO");
	}
}
